﻿using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 百词斩
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //  Class1.GetFileStruct(@"C:\Users\Administrator\Documents\leidian\Pictures\baicizhan\zpack\16\0\zp_5662_16_0_20191114174240.zpk");

            con.Open();
            if (outputDevice == null)
            {
                outputDevice = new WaveOutEvent();
                outputDevice.PlaybackStopped += OnPlaybackStopped;
            }

        }

        Microsoft.Data.Sqlite.SqliteConnection con = new Microsoft.Data.Sqlite.SqliteConnection("data source=" + Application.StartupPath + "\\word_media.db");

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            con.Close();
        }
        WaveOutEvent outputDevice;
        AudioFileReader audioFile;
        List<string> listLinks = new List<string>();
        private void button1_Click(object sender, EventArgs e)
        {
            outputDevice?.Stop();
            Index = 0;
            获取单词();

            if (listLinks.Count > 0)
            {

                try
                {
                    var subs = listLinks[Index].Split(new string[] { "$$$" }, StringSplitOptions.RemoveEmptyEntries);
                    int x = 0;
                    audioFile = new AudioFileReader(subs[x++]);
                    label1.Text = subs[x++];
                    label2.Text = subs[x++];
                    label3.Text = subs[x++];
                    label4.Text = subs[x++];
                    outputDevice.Init(audioFile);

                    outputDevice.Play();
                }
                catch (Exception ex)
                {
                    label4.Text = ex.Message;
                    timerForMoveOn.Start();
                }
            }

        }

        private void 获取单词()
        {
            listLinks.Clear();



            Microsoft.Data.Sqlite.SqliteCommand cmd = new Microsoft.Data.Sqlite.SqliteCommand();

            foreach (var item in richTextBox1.Lines)
            {
                if (item.Trim().Length > 0)
                {
                    string mycmd = "select * from tb_word_media_16 where ";
                    mycmd += " topic_word='" + item.Trim() + "'";
                    cmd.Connection = null;
                    cmd.CommandText = mycmd;

                    cmd.Connection = con;
                    var sqlReader = cmd.ExecuteReader();

                    while (sqlReader.Read())
                    {
                        string line = "http://ali.bczcdn.com" + sqlReader["m4a_audio_path"].ToString();
                        listLinks.Add(line + "$$$" + sqlReader["topic_word"].ToString() + "$$$"
                            + sqlReader["topic_word_type"].ToString() + "$$$"
                            + sqlReader["topic_word_cnmean"].ToString() + "$$$"
                            + sqlReader["topic_sentence"].ToString());

                    }
                    sqlReader.Close();
                }

            }
        }

        int Index = 0;
        private void OnPlaybackStopped(object sender, StoppedEventArgs e)
        {
            if (Index < listLinks.Count)
            {

                var subs = listLinks[Index].Split(new string[] { "$$$" }, StringSplitOptions.RemoveEmptyEntries);
                int x = 0;
                audioFile = new AudioFileReader(subs[x++]);
                label1.Text = subs[x++];
                label2.Text = subs[x++];
                label3.Text = subs[x++];
                label4.Text = subs[x++];
                outputDevice.Init(audioFile);


                outputDevice.Play();
            }
            else
            {
                //结束了，所有单词播放完毕
                label1.Text =
           label2.Text =
           label3.Text =
           label4.Text = "";
                label3.Text = "播放完毕";
                Index = 0;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Index = listLinks.Count + 10;
            下一单词();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Index = listLinks.Count + 10;
            下一单词();
        }

        private void 下一单词()
        {

            Index++;
            label1.Text =
             label2.Text =
             label3.Text =
             label4.Text = "";
            if (outputDevice.PlaybackState == PlaybackState.Playing)
            {
                outputDevice?.Stop();
            }
            else
            {
                OnPlaybackStopped(null, null);
            }

        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            label1.Font =
               label2.Font =
               label3.Font =
               label4.Font = new Font(label1.Font.FontFamily, trackBar1.Value);
        }

        private void timerForMoveOn_Tick(object sender, EventArgs e)
        {
            timerForMoveOn.Stop();
            button2_Click(null, null);

        }
    }
}
